# 作业1：

# 重新开发博客系统-有网页版
from flask import Flask, make_response, session, request, render_template, jsonify

app = Flask(__name__)

app.config.from_pyfile("config.py")

blog_list = list()



# 1、开发注册模块，编写注册页 reg.html 给网页配置path 为 /reg
@app.route("/reg", methods=["GET", "POST"])
def reg():
    response = render_template("reg.html")
    return response


# 2、开发注册模块，编写注册接口  接收网页传递来的 用户名、密码  并保存到session中

@app.route("/register", methods=["GET", "POST"])
def register():
    username = request.form.get("username")
    password = request.form.get("password")
    session["username"] = username
    session["password"] = password
    print(username)
    print(password)
    return "注册成功"


# 3、开发登录模块，编写登录页 login.html 给网页配置path 为/login

@app.route("/login", methods=["GET", "POST"])
def login():
    response = render_template("login.html")
    return response



@app.route("/log_in", methods=["GET", "POST"])
def log_in():
    # if request.method == 'POST':
        username = session.get("username")
        password = session.get("password")
        user_name = request.form.get("username")
        pass_word = request.form.get("password")
        # print(user_name)
        # print(username)
        # print(pass_word)
        # print(password)
        if username == user_name and password == pass_word:
            return render_template("shou_ye.html")
        return "用户名或密码错误"
    # if request.method == 'GET':
    #     response = render_template("login.html")
    #     return response


# 4、开发登录模块，编写登录接口  接收登录页传递来的数据，验证用户名 密码 是否与之前注册的一致。如果一致 则跳转到首页，如果不一致 返回 用户名或密码错误

# 5、开发首页模块，首页上 有 注册a标签   登录a标签 分别对应 注册页、登录页。


# 6、开发添加博客模块，编写 添加页 add.html 给网页配置path 为/add
@app.route("/add", methods=["GET", "POST"])
def add():
    response = render_template("add.html")
    return response


# 7、开发添加博客模块，编写添加接口，接收添加发送来的 标题、作者 存入到 cookie中
@app.route("/ad_d", methods=["GET", "POST"])
def ad_d():
    title = request.form.get("title")
    author = request.form.get("author")
    response = make_response("添加成功")
    response.set_cookie("title", title)
    response.set_cookie("author", author)
    print(title)
    print(author)
    return response


# 8、开发博客列表模块，编写博客列表接口，返回 获取的博客字典
@app.route("/dict", methods=["GET", "POST"])
def di_ct():
    global blog_list
    dict = {}
    title = request.cookies.get("title")
    author = request.cookies.get("author")
    dict['标题'] = title
    dict['作者'] = author
    blog_list.append(dict)
    return jsonify(blog_list)


if __name__ == '__main__':
    app.run()
